#ifndef TCPIRWORKER_H
#define TCPIRWORKER_H

#include <QObject>
class QTcpSocket;
class QHostAddress;
class PointCloudGenerator;

#include <pcl/point_cloud.h>
#include <pcl/visualization/pcl_visualizer.h>   
#include <opencv2/imgproc.hpp> 

class TcpIrWorker : public QObject
{
    Q_OBJECT
public:
    explicit TcpIrWorker(QObject *parent = nullptr);

signals:
    void connected();
    void disconnected();
    void readyReadData(const QByteArray& mess, const QHostAddress& adress);
    void errorOccurred(const QString& data);

    void depthImageReady(const QImage& image);
    void sendPointCloud(const std::shared_ptr<pcl::PointCloud<pcl::PointXYZ>>& cloud);

    void newIrDataReceived(QSharedPointer<QByteArray> data);
public slots:
    void connectToServer(const QString& ip,quint16 port);
    void sendData(const QByteArray& data);
    void onDisconnectSocket();
private slots:
    void onReadyRead();
    void processCompleteFrame(QByteArray& data);

private:
    QTcpSocket* m_socket;
    QString m_ip;
    quint16 m_port;

    QByteArray m_receiveBuffer;

    PointCloudGenerator* generator;

    void processReceiveBuffer(const QByteArray& data);
    

    QHash<QString, QVariant> parseYaml(const QString& filePath);
    quint32 convertHexStringToDecimal(const QString& hexStr);
    QByteArray hexStringToLittleEndian(const QString& hexString);
    cv::Mat hexStringToDepthMat(const QString& hexString, int width, int height);

    void parseDepthDataUint16(const QByteArray& data, QImage& image, int width, int height);
};

#define TcpIrWorkerRelease Singleton<TcpIrWorker>::Release()
#define TcpIrWorkerInstance Singleton<TcpIrWorker>::GetInstance()


#endif // TCPDEPTHWORKER_H
